﻿using DevExpress.XtraBars;
using DevExpress.XtraBars.Docking2010.Views.Widget;
using System;
using System.Threading.Tasks;
using System.Windows.Forms;
using XCI.Forms;
using XCI.Helper;

namespace XCI.Windows
{
    public partial class FrmWidgetDesign : BaseForm
    {
        private readonly string code;
        private WidgetObject widgetObject;
        public FrmWidgetDesign(string code)
        {
            InitializeComponent();
            this.code = code;
        }

        private void SetViewProperty()
        {
            propertyGridPlus1.SelectedObject = widgetObject.WidgetView;
        }

        private void SetItemProperty()
        {
            propertyGridPlus1.SelectedObject = widgetObject.WidgetView.ActiveDocument;
        }

        /// <summary>
        /// 数据加载
        /// </summary>
        private async Task LoadAsync()
        {
            await widgetObject.LoadAsync();
            SetStatus();
        }

        /// <summary>
        /// 设置按钮可用状态
        /// </summary>
        private void SetStatus()
        {
            biFlowMode.Down =
                biStackedMode.Down =
                biTableMode.Down =
                biFreeMode.Down = false;
            switch (widgetObject.WidgetView.LayoutMode)
            {
                case LayoutMode.StackLayout:
                    biStackedMode.Down = true;
                    break;
                case LayoutMode.TableLayout:
                    biTableMode.Down = true;
                    break;
                case LayoutMode.FlowLayout:
                    biFlowMode.Down = true;
                    break;
                case LayoutMode.FreeLayout:
                    biFreeMode.Down = true;
                    break;
                default:
                    biFreeMode.Down = true;
                    break;
            }

            biFlowMode.Enabled =
                biStackedMode.Enabled =
                biTableMode.Enabled =
                biFreeMode.Enabled = widgetObject.HasItem;

            biLeftToRight.Visibility
                = biRightToLeft.Visibility
                = biBottomUp.Visibility
                = biTopDown.Visibility
                = biItemMixer.Visibility = BarItemVisibility.Never;

            if (widgetObject.WidgetView.LayoutMode == LayoutMode.FlowLayout)
            {
                biLeftToRight.Visibility
                = biRightToLeft.Visibility
                = biBottomUp.Visibility
                = biTopDown.Visibility = BarItemVisibility.Always;
            }
            else if (widgetObject.WidgetView.LayoutMode == LayoutMode.TableLayout
                || widgetObject.WidgetView.LayoutMode == LayoutMode.StackLayout)
            {
                biItemMixer.Visibility = widgetObject.HasItem ? BarItemVisibility.Always : BarItemVisibility.Never;
            }
        }

        private void ApplyLayoutMode(LayoutMode layoutMode)
        {
            widgetObject.WidgetView.LayoutMode = layoutMode;
            widgetObject.WidgetView.BeginUpdateAnimation();
            LayoutMix(layoutMode);
            SetStatus();
            widgetObject.WidgetView.EndUpdateAnimation();
        }

        private void LayoutMix(LayoutMode layoutMode)
        {
            switch (layoutMode)
            {
                case LayoutMode.FlowLayout: widgetObject.FlowLayoutMix(); break;
                case LayoutMode.StackLayout: widgetObject.StackLayoutMix(); break;
                case LayoutMode.TableLayout: widgetObject.TableLayoutMix(); break;
            }
        }

        private async void FrmWidgetSetting_Load(object sender, EventArgs e)
        {
            widgetObject = new WidgetObject(widgetView1, this.code);
            await widgetObject.LoadAsync();
            SetViewProperty();
            SetStatus();
        }

        //private async void BtnAdd_ItemClick(object sender, ItemClickEventArgs e)
        //{
        //    await widgetObject.SaveLayoutAsync();
        //    var editForm = new FrmWidgetItemEdit(widgetObject, EditFormArgs.Create());
        //    editForm.ShowDialogOk(async ()=> await LoadAsync());
        //}

        //private async void BtnRemove_ItemClick(object sender, ItemClickEventArgs e)
        //{
        //    if (MessageBoxHelper.ShowWarnYesNo("确定要删除选中的部件吗?") != DialogResult.Yes) return;
        //    await widgetObject.SaveLayoutAsync();
        //    await widgetObject.RemoveItemAsync(new[] { widgetObject.WidgetView.ActiveDocument.ControlName });
        //    await LoadAsync();
        //}

        private async void BtnItems_ItemClick(object sender, ItemClickEventArgs e)
        {
            var itemsForm = new FrmWidgetItemsManager(widgetObject);
            itemsForm.ShowDialog();
            await LoadAsync();
        }

        private async void BtnOk_Click(object sender, EventArgs e)
        {
            await widgetObject.SaveLayoutAsync();
            await widgetObject.SaveItemsAsync();
            DialogOk();
        }

        private void BtnClose_Click(object sender, EventArgs e)
        {
            DialogCancel();
        }

        private async void BtnReset_Click(object sender, EventArgs e)
        {
            if (MessageBoxHelper.ShowWarnYesNo("确定要重置吗？重置后配置文件会清空") != DialogResult.Yes)
            {
                return;
            }
            await widgetObject.ResetAsync();
            MessageBoxHelper.ShowMessage("重置成功");
        }

        private void BiMode_ItemClick(object sender, ItemClickEventArgs e)
        {
            LayoutMode layoutMode = (LayoutMode)e.Item.Tag;
            ApplyLayoutMode(layoutMode);
        }

        private void BiFlowDirection_ItemClick(object sender, ItemClickEventArgs e)
        {
            FlowDirection flowDirection = (FlowDirection)e.Item.Tag;
            widgetObject.WidgetView.BeginUpdateAnimation();
            widgetObject.WidgetView.FlowLayoutProperties.FlowDirection = flowDirection;
            widgetObject.WidgetView.EndUpdateAnimation();
        }

        private void BiItemMixer_ItemClick(object sender, ItemClickEventArgs e)
        {
            LayoutMix(widgetObject.WidgetView.LayoutMode);
        }

        private void BtnViewProperty_ItemClick(object sender, ItemClickEventArgs e)
        {
            SetViewProperty();
        }

        private void WidgetView1_DocumentActivated(object sender, DevExpress.XtraBars.Docking2010.Views.DocumentEventArgs e)
        {
            if (e.Document != null)
            {
                SetItemProperty();
                SetStatus();
            }
        }
    }
}
